﻿package core.ui.dialogs
{
    import core.ui.controls.*;
    import flash.events.*;

    public class RegisterDialog extends Dialog
    {
        var fullname:TextInput;
        var email:TextInput;
        var password:TextInput;
        var passwordrepeat:TextInput;
        var message:Literal;

        public function RegisterDialog(param1:Tuyitu)
        {
            var _loc_2:Literal = null;
            super(param1, "rm_register", 240, 285, true);
            _loc_2 = new Literal("用户名");
            _loc_2.x = 18;
            _loc_2.y = 30;
            addChild(_loc_2);
            this.fullname = new TextInput(200, 800, "");
            this.fullname.x = 20;
            this.fullname.y = 47;
            addChild(this.fullname);
            _loc_2 = new Literal("rm_email");
            _loc_2.x = 18;
            _loc_2.y = 75;
            addChild(_loc_2);
            this.email = new TextInput(200, 800, "");
            this.email.x = 20;
            this.email.y = 92;
            addChild(this.email);
            _loc_2 = new Literal("rm_password");
            _loc_2.x = 18;
            _loc_2.y = 120;
            addChild(_loc_2);
            this.password = new TextInput(200, 800, "");
            this.password.t.displayAsPassword = true;
            this.password.x = 20;
            this.password.y = 137;
            addChild(this.password);
            _loc_2 = new Literal("rm_password_repeat");
            _loc_2.x = 18;
            _loc_2.y = 165;
            addChild(_loc_2);
            this.passwordrepeat = new TextInput(200, 800, "");
            this.passwordrepeat.t.displayAsPassword = true;
            this.passwordrepeat.x = 20;
            this.passwordrepeat.y = 182;
            addChild(this.passwordrepeat);
            this.message = new Literal("rm_working", true);
            this.message.x = 25;
            this.message.y = 255;
            this.message.visible = false;
            this.message.width = 200;
            addChild(this.message);
            param1.stage.focus = this.fullname.t;
            return;
        }// end function

        override public function CloseClick(event:MouseEvent) : void
        {
            Close();
            ed.CheckToShowSplash();
            return;
        }// end function

        override public function OKClick(event:MouseEvent) : void
        {
            if (this.fullname.value == "" || this.email.value == "" || this.password.value == "" || this.passwordrepeat.value == "")
            {
                new AlertDialog(ed, "rm_insert_data");
            }
            else if (this.password.value != this.passwordrepeat.value)
            {
                new AlertDialog(ed, "rm_password_mismatch");
            }
            else
            {
                var _loc_2:Boolean = false;
                cancel.visible = false;
                ok.visible = _loc_2;
                this.message.visible = true;
                U.CallHome("register", {name:this.fullname.value, email:this.email.value, password:this.password.value, password_repeat:this.passwordrepeat.value}, this.Callback);
            }
            return;
        }// end function

        public function Callback(displayAsPassword:Object) : void
        {
            U.SetLocalData("email", this.email.text);
            var _loc_2:Boolean = true;
            cancel.visible = true;
            ok.visible = _loc_2;
            this.message.visible = false;
            if (displayAsPassword.success)
            {
                U.Log("rm_register");
                ed.cm.LoginUser({name:displayAsPassword.payload.name, email:displayAsPassword.payload.email});
                Close();
                ed.CheckToShowSplash();
                new AlertDialog(ed, "rm_welcome_to_tuyitu");
            }
            else
            {
                switch(String(displayAsPassword.payload).toLowerCase())
                {
                    case "err:email_in_use":
                    {
                        new AlertDialog(ed, "rm_email_exists");
                        break;
                    }
                    default:
                    {
                        new AlertDialog(ed, "rm_error_occurred");
                        break;
                    }
                }
            }
            return;
        }// end function

    }
}
